package top.syfy.spb.upload.imageandwatermark.service.impl;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.stereotype.Service;
import top.syfy.spb.upload.imageandwatermark.constant.WatermarkEnum;
import top.syfy.spb.upload.imageandwatermark.service.WatermarkService;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.AlphaComposite;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 给上传的图片添加水印的服务层接口实现
 *
 * @author awg
 * @date 2020/10/23 星期五
 */
@Service
public class WatermarkServiceImpl implements WatermarkService {
    /**
     * 给上传的图片添加水印
     *
     * @param uploadedImg      已上传的待添加水印的图片
     * @param watermarkImgPath 服务器中水印图片的物理存储路径
     */
    @Override
    public String watermarkAdd(File uploadedImg, String watermarkImgPath) throws IOException {
        // 已上传的待添加水印的图片的名称
        String imageFileName = uploadedImg.getName();
        // 添加水印之后的图片的名称: abc.png  ==> abc_watermarked.png
        // 替换规则：只需要将【abc.png】中的【.】替换为【WatermarkEnum.WATERMARK_WATERMARKED_TEXT的值(_watermarked.)即可】
        String markedImgName = imageFileName.replace(String.valueOf(imageFileName.charAt(imageFileName.lastIndexOf("."))), String.valueOf(WatermarkEnum.getEnumValueByName("WATERMARK_WATERMARKED_TEXT")));
        /*
            读取已上传的待添加水印的图片
         */
        Image image = ImageIO.read(uploadedImg);
        int uploadedImgWidth = image.getWidth(null);
        int uploadedImgHeight = image.getHeight(null);
        // 创建图片缓存对象
        BufferedImage bufferedImage = new BufferedImage(uploadedImgWidth, uploadedImgHeight, BufferedImage.TYPE_INT_RGB);
        // 创建绘绘图工具对象
        Graphics2D g = bufferedImage.createGraphics();
        // 使用绘图工具将原图绘制到缓存图片对象
        g.drawImage(image, 0, 0, uploadedImgWidth, uploadedImgHeight, null);
         /*
            读取服务器中的水印图片
         */
        Image watermarkImg = ImageIO.read(new File(watermarkImgPath));
        // 水印图片的宽度和高度
        int watermarkImgWidth = watermarkImg.getWidth(null);
        int watermarkImgHeight = watermarkImg.getHeight(null);
        // 设置水印透明度
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Float.parseFloat(String.valueOf(WatermarkEnum.getEnumValueByName("WATERMARK_ALPHA")))));
        // 旋转图片
        g.rotate(Math.toRadians(-10), bufferedImage.getWidth() / 2.0, bufferedImage.getHeight() / 2.0);
        // 水印添加位置的x, y坐标
        int xLocation = Integer.parseInt(String.valueOf(WatermarkEnum.getEnumValueByName("WATERMARK_LOCATION_X")));
        int yLocation = Integer.parseInt(String.valueOf(WatermarkEnum.getEnumValueByName("WATERMARK_LOCATION_Y")));
        // 水印添加位置的x, y轴上每个水印的间隔
        int xInterval = Integer.parseInt(String.valueOf(WatermarkEnum.getEnumValueByName("WATERMARK_INTERVAL_X")));
        int yInterval = Integer.parseInt(String.valueOf(WatermarkEnum.getEnumValueByName("WATERMARK_INTERVAL_Y")));

        double count = 1.5;
        // 循环添加水印
        while (xLocation < uploadedImgWidth * count) {
            yLocation = -uploadedImgHeight / 2;
            while (yLocation < uploadedImgHeight * count) {
                // 添加水印
                g.drawImage(watermarkImg, xLocation, yLocation, null);
                yLocation += watermarkImgHeight + yInterval;
            }
            xLocation += watermarkImgWidth + xInterval;
        }
        g.dispose();
        String markedImgPath = uploadedImg.getParent() + File.separator + markedImgName;
        OutputStream os = new FileOutputStream(markedImgPath);
        JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
        en.encode(bufferedImage);
        os.close();
        return markedImgPath;
    }
}
